package com.dearbinge.opentapi.content.im;

import com.baomidou.mybatisplus.plugins.Page;
import com.dearbinge.data.api.im.UserContactTagRelationService;
import com.dearbinge.data.pojo.im.ImUserContactTagRelation;
import com.dearbinge.utils.ResultMsg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;


/**
 * 标签内好友关系控制层
 *
 * @author wes
 * @date 2019/2/27
 */
@Api(tags = "5.4、通讯-好友关系标签")
@RestController
@RequestMapping("/userContactTagRelation")
@CrossOrigin
@EnableAutoConfiguration
public class UserContactTagRelationController extends WebMvcConfigurerAdapter {

    @Autowired
    private UserContactTagRelationService userContactTagRelationService;

    /**
     * 根据ID删除
     */
    @ApiOperation(value = "根据ID删除", notes = "根据ID删除")
    @ApiImplicitParam(name = "id", value = "id", paramType = "query",required = true)
    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    public ResultMsg<String> delete(Integer id) {
        try {
            Integer dataCode = userContactTagRelationService.delete(id);
            if (dataCode > 0){
                return ResultMsg.ok("删除成功");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }

    /**
     * 新增/修改
     */
    @ApiOperation(value = "新增/修改",notes = "新增/修改")
    @RequestMapping(value = "addOrUpdate",method = RequestMethod.POST)
    public ResultMsg<String> addOrUpdate(@RequestBody ImUserContactTagRelation userContactTagRelation){
        try {
            if (userContactTagRelation.getId() != null && userContactTagRelation.getId() != 0){
                Integer dataCode = userContactTagRelationService.update(userContactTagRelation);
                if (dataCode > 0){
                    return ResultMsg.ok("修改成功");
                }
            }else{
                Integer dataCode = userContactTagRelationService.insert(userContactTagRelation);
                if (dataCode > 0){
                    return ResultMsg.ok("新增成功");
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }

    /**
     * 根据ID查询
     */
    @ApiOperation(value = "根据ID查询对象",notes = "根据ID查询")
    @ApiImplicitParam(name = "id",value = "id",paramType = "query")
    @RequestMapping(value = "/getById",method = RequestMethod.GET)
    public ResultMsg<ImUserContactTagRelation> getById(Integer id){
        try {
            ImUserContactTagRelation userContactTag = userContactTagRelationService.getById(id);
            return ResultMsg.ok(userContactTag);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }



    /**
     * 根据标签ID查询标签组成员
     */
    @ApiOperation(value = "根据标签ID查询标签组成员",notes = "根据标签ID查询标签组成员")
    @ApiImplicitParam(name = "tagId",value = "tagId",paramType = "query")
    @RequestMapping(value = "/listByTagId",method = RequestMethod.GET)
    public ResultMsg<Page> listByTagId(Integer tagId, Integer current, Integer size){
        try {
            Page page  = userContactTagRelationService.listByTagId(tagId, current,  size);
            return ResultMsg.ok(page);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }





}
